//
//  CategoriesTableViewCell.m
//  OneDay
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "CategoriesTableViewCell.h"

@implementation CategoriesTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubviews];
    }
    return self;
}

// 添加子视图
- (void)addSubviews
{
    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 2.26)];
    // 打开交互
    self.bgImageView.userInteractionEnabled = YES;
    [self addSubview:self.bgImageView];
    
    CGFloat kSpace = kWidth / 15;
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSpace, kSpace * 8, kWidth - kSpace * 2, self.bgImageView.height / 4.9)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:22];
    self.titleLabel.numberOfLines = 2;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.bgImageView addSubview:self.titleLabel];
    
    self.categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.8 * kWidth, 10, 0.2 * kWidth, 30)];
    self.categoryLabel.textColor = [UIColor brownColor];
    [self.bgImageView addSubview:self.categoryLabel];
    
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectionButton.frame = CGRectMake(self.titleLabel.left + 5, self.titleLabel.bottom + 5, self.bgImageView.width / 15, 20);
    [self.collectionButton addTarget:self action:@selector(collectionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImageView addSubview:self.collectionButton];
    self.collectionLabel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectionLabel.frame = CGRectMake(self.collectionButton.right, self.titleLabel.bottom, self.bgImageView.width / 5, 30);
    [self.collectionLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.collectionLabel addTarget:self action:@selector(collectionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImageView addSubview:self.collectionLabel];
    
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareButton.frame = CGRectMake(self.collectionLabel.right + 5, self.titleLabel.bottom + 5, self.bgImageView.width / 15, 20);
    [self.bgImageView addSubview:self.shareButton];
    self.shareLabel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareLabel.frame = CGRectMake(self.shareButton.right, self.titleLabel.bottom, self.bgImageView.width / 5, 30);
    [self.shareLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.bgImageView addSubview:self.shareLabel];
}

- (void)setModel:(VideoModel *)model
{
    if (_model != model) {
        _model = model;
    }
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:self.model.coverForDetail] placeholderImage:[UIImage imageNamed:@"oneDay.jpg"]];
    self.titleLabel.text = self.model.title;
    self.categoryLabel.text = [NSString stringWithFormat:@"#%@", self.model.category];
    [self.collectionButton setImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:UIControlStateNormal];
    [self.collectionLabel setTitle:[NSString stringWithFormat:@"%@", self.model.consumption[@"collectionCount"]] forState:UIControlStateNormal];
    [self.shareButton setImage:[UIImage imageNamed:@"iconfont-fenxiang-2"] forState:UIControlStateNormal];
    [self.shareLabel setTitle:[NSString stringWithFormat:@"%@", self.model.consumption[@"shareCount"]] forState:UIControlStateNormal];
}

- (void)collectionBtnClick
{
    if ([self.delegate respondsToSelector:@selector(collectionCellDidSelected:)]) {
        [self.delegate collectionCellDidSelected:self.model];
    }
}

@end
